package com.dk.controller;


import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dk.Dto.PartPower;
import com.dk.Util.JwtUtil;
import com.dk.pojo.Menu;
import com.dk.pojo.Menuandpart;
import com.dk.pojo.RESTFUL;
import com.dk.pojo.Users;
import com.dk.service.IMenuService;
import com.dk.service.IMenuandpartService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-07-08
 */
@RestController
@RequestMapping("/menu")
public class MenuController {


    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuandpartService iMenuandpartService;
    @GetMapping("getmenu")
    @ApiOperation(value = "获得菜单")
    public RESTFUL<List<Menu>> getmenu(HttpSession httpSession, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        DecodedJWT token1 = JwtUtil.getToken(token);
        Claim idString = token1.getClaim("id");
        Map<String,Object> map = new HashMap<>();
        Integer id = Integer.valueOf(idString.asString());
        Users user =(Users) httpSession.getAttribute("user");
        System.out.println(id);
        return new RESTFUL().SUCCESS(menuService.getMenu(id).stream().distinct());
    }

    @GetMapping("getmenuall")
    @ApiOperation(value = "获得所有菜单")
    public RESTFUL<List<Menu>> getallmenu(){

        return new RESTFUL().SUCCESS(menuService.getMenuAll());
    }


    @PostMapping("addmenu")
    @ApiOperation(value = "增加菜单")
    public RESTFUL addMenu(@RequestBody Menu menu){
        menuService.save(menu);
        return new RESTFUL().SUCCESS();
    }
    @GetMapping("dele")
    @ApiOperation(value = "删除菜单")
    public RESTFUL delMenu(@ApiParam("被删除的id") Integer id){
        menuService.removeById(id);
        return new RESTFUL().SUCCESS();
    }
    @PostMapping("delelist")
    @ApiOperation(value = "批量删除菜单")
    public RESTFUL delMenu(@RequestBody List<Integer> idlist){
        menuService.removeByIds(idlist);
        return new RESTFUL().SUCCESS();
    }
    @GetMapping("select")
    @ApiOperation(value = "菜单分页")
    public RESTFUL seleMenu(
            @ApiParam("名称") String name,
            @ApiParam("状态") String status,
            @ApiParam("姓名") String type,
            @ApiParam("页码") Integer ipage,
            @ApiParam("页容量") Integer size
    ){

        Menu menu=new Menu();
        menu.setName(name);
        menu.setStart(status);
        menu.setType(type);

        Page page=new Page(ipage,size);
        return new RESTFUL().SUCCESS(menuService.getMenuList(page,menu));
    }
    @GetMapping("getpart")
    @ApiOperation(value = "获得父级菜单")
    public RESTFUL getpart(){

        return new RESTFUL().SUCCESS(menuService.getPartList());
    }

    @GetMapping("gethavepart")
    @ApiOperation(value = "获得父级菜单")
    public RESTFUL gethavepart(Integer id){

        return new RESTFUL().SUCCESS(menuService.getHaveMenu(id));
    }

    @PostMapping("partpower")
    @Transactional
    @ApiOperation(value = "设置角色权限")
    public RESTFUL gavePatt(@RequestBody PartPower partPower){
        List<Menuandpart> list=new ArrayList<>();
        List<Integer> menuIds = partPower.getMenuIds();
        for (Integer menuId : menuIds) {
            Menuandpart menuas=new Menuandpart();
            menuas.setPartid(partPower.getPartId());
            menuas.setMenuid(menuId);
            list.add(menuas);
        }
        QueryWrapper<Menuandpart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("partid",partPower.getPartId());
        iMenuandpartService.remove(queryWrapper);
        iMenuandpartService.saveBatch(list);

        return new RESTFUL().SUCCESS();

    }
    @PostMapping("updatamenu")
    @ApiOperation(value = "修改菜单")
    public RESTFUL updataMenu(@RequestBody Menu menu){
        menuService.updateById(menu);

        return new RESTFUL().SUCCESS();

    }
}
